package com.mmall.controller.admin;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.ICategoryService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA
 * Created By Kinsey
 * Date: 2017/12/26
 * Time: 22:44
 */
@RequestMapping("/manager/category/")
@Controller
public class CategoryController {
    @Autowired
    private IUserService iUserService;

    @Autowired
    private ICategoryService iCategoryService;

    @RequestMapping(value = "add_category")
    @ResponseBody
    public ServerResponse addCategory(HttpSession session,String categoryName,@RequestParam(value = "parentId",defaultValue = "0") Integer parentId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
            //判断是否是管理员
         if(iUserService.checkAdminRole(user).isSuccess()){
             //是管理员  增加分类信息
             return iCategoryService.addCategory(categoryName,parentId);
         }else {
             return ServerResponse.createByErrorOnMsg("你是不管理员，无权限操作！");
         }

    }

    @RequestMapping(value = "update_category")
    @ResponseBody
    public  ServerResponse updateCategoryName(HttpSession session,Integer categoryId ,String categoryName){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
        //判断是否是管理员
        if(iUserService.checkAdminRole(user).isSuccess()){
            //是管理员  更新分类信息
            return iCategoryService.updateCategory(categoryName,categoryId);
        }else {
            return ServerResponse.createByErrorOnMsg("你是不管理员，无权限操作！");
        }
    }

    /**
     * 查询子节点的category，并且不递归，保持平级
     * @param session
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "get_childercategory")
    @ResponseBody
    public ServerResponse getChilderCategory(HttpSession session,@RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
        //判断是否是管理员
        if(iUserService.checkAdminRole(user).isSuccess()){
            //查询子节点的category，并且不递归，保持平级
            return iCategoryService.getChilderCategory(categoryId);
        }else {
            return ServerResponse.createByErrorOnMsg("你是不管理员，无权限操作！");
        }
    }

    /**
     * 查询当前的ID和递归子节点的ID
     * @param session
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "get_childercategory")
    @ResponseBody
    public ServerResponse getDeepChilderCategory(HttpSession session,@RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
        //判断是否是管理员
        if(iUserService.checkAdminRole(user).isSuccess()){
            //查询当前的ID和递归子节点的ID
            return iCategoryService.selectCategoryAndChilderById(categoryId);
        }else {
            return ServerResponse.createByErrorOnMsg("你是不管理员，无权限操作！");
        }
    }
}
